package com.rem.concurrency.multithreading.base;

/**
 * 多个线程操作同一个对象
 * 买火车票的例子
 *
 * @author Rem
 * @date 2020-10-26
 */

public class Demo3 implements Runnable {

    private int ticketNum = 10;

    //synchronized 同步
    @Override
    public synchronized void run() {
    while (ticketNum > 0) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + "拿到了第" + ticketNum-- + "张票");
        }
    }

    public static void main(String[] args) {
        Demo3 demo3 = new Demo3();

        new Thread(demo3, "小明").start();
        new Thread(demo3, "张三").start();
        new Thread(demo3, "黄牛").start();

/*
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());



        singleThreadPool.execute(()-> System.out.println(Thread.currentThread().getName()));
        singleThreadPool.shutdown();*/
    }
}
